﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleMaintenance.Framework;
using VehicleMaintenance.Models;
using VehicleMaintenance.Comm;

namespace VehicleMaintenance.BaseInfo
{
    public partial class StaffInfoForm : AddOrEditBaseForm
    {
        /// <summary>
        /// 记录当前员工ID
        /// </summary>
        private int staffID = 0;

        #region 构造函数

        public StaffInfoForm()
        {
            InitializeComponent();
			ucFormInput1.InitializeControls(typeof(Models.Staff));
        }

        public StaffInfoForm( int id)
        {
            InitializeComponent();
			ucFormInput1.InitializeControls(typeof(Models.Staff));
            this.state = State.Edit;
            staffID = id;
        }

        #endregion 构造函数

        private void StaffInfoForm_Load(object sender, EventArgs e)
        {
            try
            {
                if (this.state == State.Edit)
                {
                    CarDBContext db = new CarDBContext();

                    Staff staff = db.Staff.First(p => p.StaffID == this.staffID);
                    if (staff != null)
                    {
                        ucFormInput1.SetValues(staff);
                    }


                }
                else
                {
					ucFormInput1.ClearData();
                }
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
				if (!ucFormInput1.VerifyInput())
				{
					return;
				}
                CarDBContext db = new CarDBContext();

                if (this.state == State.Edit)
                {
                    Staff staff = db.Staff.First(p => p.StaffID == this.staffID);
                    ucFormInput1.GetValues(staff);

                    db.SaveChanges();

                    this.ShowInfo("保存成功！");
                }
                else
                {
                    //查询员工编码是否重复
                    string strCode = ucFormInput1.GetControlByName("Code").Text;
                    if (db.Staff.SingleOrDefault(p => p.Code == strCode) != null)
                    {
                        this.ShowInfo("员工编码已经存在");
                        //tabPanel.Controls["Code"]();
                        ucFormInput1.GetControlByName("Code").Focus();
                        return;
                    }
                    var staff = db.Staff.Create();
                    ucFormInput1.GetValues(staff);

                    db.Staff.Add(staff);
                    db.SaveChanges();

                    if (MessageBox.Show(this, "保存成功，是否继续添加？", "提示", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    {
                        this.ClearData(this.panel1);
                        ucFormInput1.GetControlByName("Code").Focus();
                    }
                    else
                    {
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    }
                }

            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }


    }
}

